package com.jt.controller;

import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description:
 * @author: LiuXingYu
 * @date: 2021/7/29 14:19
 */
@RestController
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 1.restFul实现用户查询
     * URL: http://localhost:8090/user/100
     * type: GET
     * RequestMapping 默认的可以接收所有的请求类型
     * RestFul语法:
     *      1.参数的位置固定.
     *      2.参数必须使用{}包裹
     *      3.必须使用@PathVariable 动态的接收参数
     *      注意事项: {参数名称}必须与方法中的名称一致.
     *
     * RestFul优化:
     *      如果{参数名}和对象的属性名一致
     *      则SpringMVC动态的为对象赋值
     *      其中@PathVariable可以省略
     *
     * 注意:前后端传递参数要保持一致
     */
    //@RequestMapping(value = "/user", method = RequestMethod.GET)
    @GetMapping("/user/{id}")
    public String RestFulGet(@PathVariable Integer id){
        return "" + id;
    }

    @GetMapping("/findAll")
    public List<User> findAll(){
        return userService.findAll();
    }

    @DeleteMapping("deleteUserById/{id}")
    public String deleteUserById(@PathVariable Integer id){
        return userService.deleteUserById(id);
    }

    @PostMapping("/insertUser")
    public String insertUser(@RequestBody User user){
        return userService.insertUser(user);
    }

    @PutMapping("/updateUser")
    public String updateUser(@RequestBody User user){
        System.out.println(user);
        return userService.updateUser(user);
    }
}
